/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dttung
 */
@WebServlet(name="ShowMatch", value={"/ShowMatch","/index.jsp"}, initParams={@WebInitParam(name="tours", value="1")})
public class ShowMatch extends HttpServlet{
    private final int nTours = 38;
    // Method to handle GET method request.
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        ServletUtil.printFistHalf(out);
        String tours = request.getParameter("tours");
        
        int selectedTour = 1;
        if (tours != null)
            selectedTour = Integer.valueOf(tours); // 1-index
        printForm(out, selectedTour);
        
        //out.println("<p>Lịch đấu / Kết quả của vòng " + String.valueOf(selectedTour) + "</p>");
        Utils myUtil = new Utils();
        ArrayList<String> matches = new ArrayList<String>(); 
        try {
            myUtil.readYffScores();
            myUtil.readTeamLinks();
            matches = myUtil.getMatches(selectedTour); // because of 0-index
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ShowMatch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(ShowMatch.class.getName()).log(Level.SEVERE, null, ex);
        }
        out.println("<table border=\"1\" bordercolor=\"#0000CC\" style=\"background-color:#FFFFCC\" width=\"100%\" cellpadding=\"5\" cellspacing=\"1\">\n" +
                    "	<tr>\n" +
                    "		<th align=\"right\">Chủ nhà</td>\n" +
                    "		<th align=\"center\">Kết quả</td>\n" +
                    "		<th align=\"left\">Khách</td>\n" +
                    "	</tr>\n");


        for (int i = 0; i < matches.size(); i ++)
        {
            String match = matches.get(i);            
            int pos = match.indexOf("|");
            String team1 = match.substring(0, pos);
            String team2 = match.substring(pos + 1, match.length());
            // get score
            String[] yffscores1 = (String[])myUtil.yffscores.get(team1);
            String t1score = yffscores1[selectedTour - 1]; // because of 0-index
            String[] yffscores2 = (String[])myUtil.yffscores.get(team2);
            String t2score = yffscores2[selectedTour - 1]; // because of 0-index
            //out.println("<p><strong>" + team1 + "</strong>&nbsp" + t1score + "&nbsp vs &nbsp" + t2score + "&nbsp<strong>" + team2 + "</strong></p>");
            if ( Math.abs(Float.valueOf(t1score) - Float.valueOf(t2score)) < 5 )
            {
                out.println("	<tr>\n" +
                            "		<td align=\"right\"><strong>" + myUtil.getTeamLinkHtml(team1,"blue") + "</strong></td>\n" +
                            "		<td align=\"center\">" + t1score + "&nbsp-&nbsp" + t2score + "</td>\n" +
                            "		<td align=\"left\"><strong>" + myUtil.getTeamLinkHtml(team2,"blue") + "</strong></td>\n" +
                            "	</tr>\n");
            } else if ((Float.valueOf(t1score) - Float.valueOf(t2score)) >= 5)
            {
                out.println("	<tr>\n" +
                            "		<td align=\"right\"><strong>" + myUtil.getTeamLinkHtml(team1,"red") + "</strong></td>\n" +
                            "		<td align=\"center\">" + t1score + "&nbsp-&nbsp" + t2score + "</td>\n" +
                            "		<td align=\"left\">" + myUtil.getTeamLinkHtml(team2,"blue") + "</td>\n" +
                            "	</tr>\n");
            } else
            {
                out.println("	<tr>\n" +
                            "		<td align=\"right\">" + myUtil.getTeamLinkHtml(team1,"blue") + "</td>\n" +
                            "		<td align=\"center\">" + t1score + "&nbsp-&nbsp" + t2score + "</td>\n" +
                            "		<td align=\"left\"><strong>" + myUtil.getTeamLinkHtml(team2,"red") + "</strong></td>\n" +
                            "	</tr>\n");
            }
            
        }
        out.println("</table>\n");
        ServletUtil.printSecondHalf(out);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
    public void printForm(PrintWriter out, int selIndex)
    {
        out.println("<form action=\"ShowMatch\" method=\"POST\">\n" +
"                <strong>Lịch đấu kết quả &nbsp</strong> 	\n" +
"                <select name=\"tours\" onchange=\"this.form.submit()\">\n");
        for (int i = 0; i < nTours; i++)
        {
            int value = i + 1;
            if (value == selIndex)
                out.println("                                    <option value=\"" + value + "\" selected>Vòng " + value + "</option>\n");
            else
                out.println("                                    <option value=\"" + value + "\">Vòng " + value + "</option>\n");
        }
        out.println(
"                </select>\n" +
"                <br />\n" +
"            </form>");
    }
}
